﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using DRHotel_v1.Classes;
using DRHotel_v1.Classes.DBManagers;

namespace DRHotel_v1.HousekeepingAndStaffMngmt
{
    public partial class RoomManagement : Form
    {
        public RoomManagement()
        {
            InitializeComponent();

            //Setting up comboboxes
            cbxSearchBy.SelectedIndex = 0;
            cbxSearchBy2.SelectedIndex = 0;
        }

        private void btnCreate2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to create this item?", "Confirmation Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // a 'DialogResult.Yes' value was returned from the MessageBox
                // proceed with your deletion

                #region declare room
                Room room = new Room();
                room.roomID = Convert.ToInt32(tbxRoomID.Text);
                room.capacity = Convert.ToInt32(tbxCapacity.Text);
                room.roomRates = Convert.ToInt32(tbxRoomRates.Text);
                room.roomStatus = "Vacant";

                #region room type
                if (rbnSuperior.Checked)
                {
                    room.roomType = "Superior Room";
                }
                else if (rbnDeluxe.Checked)
                {
                    room.roomType = "Deluxe Suite";
                }
                else if (rbnExecutive.Checked)
                {
                    room.roomType = "Executive Suite";
                }
                else if (rbnPresidential.Checked)
                {
                    room.roomType = "Presidential Suite";
                }
                #endregion

                #region bed type
                if (rbnSingle.Checked)
                {
                    room.bedType = "Double";
                }
                else if (rbnQueen.Checked)
                {
                    room.bedType = "Queen";
                }
                else if (rbnKing.Checked)
                {
                    room.bedType = "King";
                }
                #endregion

                #region smoking
                if (rbnSmoking.Checked)
                {
                    room.smoking = true;
                }
                else if (rbnNonSmoking.Checked)
                {
                    room.smoking = false;
                }
                #endregion

                #endregion

                #region check existing room & insert room
                //Check for existing room 
                ArrayList allRoom = HousekeepingDBManager.GetAllRoomList();
                bool insert = true;
                foreach (Room room2 in allRoom)
                {
                    if (room2.roomID == room.roomID)
                    {
                        MessageBox.Show("Room already exists!");
                        insert = false;
                        break;
                    }
                }

                //If room doesnt exist, insert room
                if (insert == true)
                {
                    bool success = HousekeepingDBManager.InsertRoom(room);

                    if (success == true)
                    {
                        MessageBox.Show("Room created successfully!");

                    }
                    else
                    {
                        MessageBox.Show("Room not created, please try again!");
                    }
                }
                #endregion
            }
        }

        private void cbxSearchBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            string searchBy = cbxSearchBy.SelectedItem.ToString();

            //Defining what the user search by
            if (searchBy.Equals("Room ID"))
            {
                ArrayList roomList = HousekeepingDBManager.GetRoomList();
                cbxSearch.Items.Clear();

                for (int i = 0; i < roomList.Count; i++)
                {
                    Room room2 = (Room)roomList[i];
                    cbxSearch.Items.Add(room2.roomID);
                }
            }
            else if (searchBy.Equals("Room Type"))
            {
                cbxSearch.Items.Clear();
                cbxSearch.Items.Add("Superior Room");
                cbxSearch.Items.Add("Deluxe Suite");
                cbxSearch.Items.Add("Executive Suite");
                cbxSearch.Items.Add("Presidential Suite");
            }
            else if (searchBy.Equals("Room Bed Type"))
            {
                cbxSearch.Items.Clear();
                cbxSearch.Items.Add("Double Single");
                cbxSearch.Items.Add("Queen");
                cbxSearch.Items.Add("King");
            }
            else if (searchBy.Equals("Room Status"))
            {
                cbxSearch.Items.Clear();
                cbxSearch.Items.Add("Vacant");
                cbxSearch.Items.Add("Occupied");
                cbxSearch.Items.Add("Reserved");
                cbxSearch.Items.Add("Scheduled for cleaning");
            }
            else if (searchBy.Equals("Room Smoking"))
            {
                cbxSearch.Items.Clear();
                cbxSearch.Items.Add("Yes");
                cbxSearch.Items.Add("No");
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string value = Convert.ToString(cbxSearch.SelectedItem);
            string type = cbxSearchBy.SelectedItem.ToString();

            RoomManagementRetrievedFeedback form = new RoomManagementRetrievedFeedback(type, value);
            form.ShowDialog();
        }

        private void btnRetrieveAll_Click(object sender, EventArgs e)
        {
            string type = "All";
            string value = "All";

            RoomManagementRetrievedFeedback form = new RoomManagementRetrievedFeedback(type, value);
            form.ShowDialog();
        }

        private void btnSearch2_Click(object sender, EventArgs e)
        {
            string searchBy = cbxSearchBy2.SelectedItem.ToString();
            if (searchBy.Equals("Room ID"))
            {
                int roomID = Convert.ToInt32(cbxSearch2.SelectedItem.ToString());
                Room room = HousekeepingDBManager.GetRoomByID(roomID);
                #region inputing data into form
                tbxRoomID3.Text = room.roomID.ToString();
                tbxCapacity3.Text = room.capacity.ToString();
                tbxRoomRates3.Text = room.roomRates.ToString();

                #region room type
                if (room.roomType.ToString().Equals("Superior Room"))
                {
                    cbxRoomType3.SelectedIndex = 0;
                }
                else if (room.roomType.ToString().Equals("Deluxe Suite"))
                {
                    cbxRoomType3.SelectedIndex = 1;
                }
                else if (room.roomType.ToString().Equals("Executive Suite"))
                {
                    cbxRoomType3.SelectedIndex = 2;
                }
                else if (room.roomType.ToString().Equals("Presidential Suite"))
                {
                    cbxRoomType3.SelectedIndex = 3;
                }
                #endregion

                #region bed type
                if (room.bedType.ToString().Equals("Double"))
                {
                    cbxBedType3.SelectedIndex = 0;
                }
                else if (room.bedType.ToString().Equals("Queen"))
                {
                    cbxBedType3.SelectedIndex = 1;
                }
                else if (room.bedType.ToString().Equals("King"))
                {
                    cbxBedType3.SelectedIndex = 2;
                }
                #endregion

                #region smoking
                if (room.smoking.ToString().Equals("False"))
                {
                    cbxSmoking3.SelectedIndex = 0;
                }
                else if (room.smoking.ToString().Equals("True"))
                {
                    cbxSmoking3.SelectedIndex = 1;
                }
                #endregion

                #region room status
                if (room.roomStatus.ToString().Equals("Vacant"))
                {
                    cbxRoomStatus.SelectedIndex = 0;
                }
                else if (room.roomStatus.ToString().Equals("Occupied"))
                {
                    cbxRoomStatus.SelectedIndex = 1;
                }
                else if (room.roomStatus.ToString().Equals("Schedule for Cleaning"))
                {
                    cbxRoomStatus.SelectedIndex = 2;
                }
                #endregion
                #endregion
            }
        }

        private void cbxSearchBy2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string searchBy = cbxSearchBy2.SelectedItem.ToString();

            //Defining what the user search by
            if (searchBy.Equals("Room ID"))
            {
                ArrayList roomList = HousekeepingDBManager.GetRoomList();
                cbxSearch2.Items.Clear();

                for (int i = 0; i < roomList.Count; i++)
                {
                    Room room2 = (Room)roomList[i];
                    cbxSearch2.Items.Add(room2.roomID);
                }
            }
        }

        private void btnUpdateRoom_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to update this room?", "Confirmation Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // a 'DialogResult.Yes' value was returned from the MessageBox
                // proceed with your deletion

                //Declaring variables
                int roomID = Convert.ToInt32(tbxRoomID3.Text);
                int capacity = Convert.ToInt32(tbxCapacity3.Text);
                string roomType = cbxRoomType3.SelectedItem.ToString();
                string bedType = cbxBedType3.SelectedItem.ToString();
                string roomStatus = cbxRoomStatus.SelectedItem.ToString();
                double roomRates = Convert.ToDouble(tbxRoomRates3.Text);

                #region smoking 
                bool smoking = false;
                if (cbxSmoking3.SelectedItem.ToString().Equals("No"))
                {
                    smoking = false;
                }
                else if (cbxSmoking3.SelectedItem.ToString().Equals("Yes"))
                {
                    smoking = true;
                }
                #endregion

                #region check existing room & update room
                //Check for existing room
                ArrayList allRoom = HousekeepingDBManager.GetAllRoomList();
                bool insert = true;
                foreach (Room room2 in allRoom)
                {
                    if (room2.roomID == roomID)
                    {
                        MessageBox.Show("Room already exists!");
                        insert = false;
                        break;
                    }
                }

                //If room doesnt exist, update the room
                if (insert == true)
                {
                    int success = HousekeepingDBManager.UpdateRoomByID(roomID, roomType, bedType, capacity, roomRates, roomStatus, smoking);

                    if (success == 1)
                    {
                        MessageBox.Show("Room updated successfully!");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Room not updated, please try again!");
                    }
                }
                #endregion
            }
        }

        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        // Validation of user input
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        private void tbxRoomID_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxRoomID.Text))
            {
                MessageBox.Show("Room ID required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxRoomID.SelectAll();
                tbxRoomID.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxRoomID.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Room ID invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxRoomID.SelectAll();
                tbxRoomID.Focus();
                return;
            }
        }

        private void tbxCapacity_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxCapacity.Text))
            {
                MessageBox.Show("Room capacity required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxCapacity.SelectAll();
                tbxCapacity.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxCapacity.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Room capacity invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxCapacity.SelectAll();
                tbxCapacity.Focus();
                return;
            }
        }

        private void tbxRoomRates_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxRoomRates.Text))
            {
                MessageBox.Show("Room rates required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxCapacity.SelectAll();
                tbxCapacity.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxRoomRates.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Room rates invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxCapacity.SelectAll();
                tbxCapacity.Focus();
                return;
            }
        }

        private void tbxRoomID3_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxRoomID3.Text))
            {
                MessageBox.Show("Room ID required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxRoomID.SelectAll();
                tbxRoomID.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxRoomID3.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Room ID invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxRoomID.SelectAll();
                tbxRoomID.Focus();
                return;
            }
        }

        private void tbxCapacity3_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxCapacity3.Text))
            {
                MessageBox.Show("Room capacity required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxCapacity.SelectAll();
                tbxCapacity.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxCapacity3.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Room capacity invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxCapacity.SelectAll();
                tbxCapacity.Focus();
                return;
            }
        }

        private void tbxRoomRates3_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxRoomRates3.Text))
            {
                MessageBox.Show("Room rates required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxCapacity.SelectAll();
                tbxCapacity.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxRoomRates3.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Room rates invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxCapacity.SelectAll();
                tbxCapacity.Focus();
                return;
            }
        }
        
    }
}
